package cn.kgc.ywz.smbms.web.web;

import cn.kgc.ywz.smbms.web.entity.Bill;
import cn.kgc.ywz.smbms.web.entity.Provider;
import cn.kgc.ywz.smbms.web.service.BillService;
import cn.kgc.ywz.smbms.web.service.ProviderService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/bill")
public class BillController {

    @Resource
    private BillService billService;

    @Resource
    private ProviderService providerService;

    /**
     * 查询
     */
    @RequestMapping("/show")
    public String toBill(Bill condition, Model model, Integer pageNum, Integer pageSize) {
        pageSize = 5;
        PageInfo<Bill> pageInfo = this.billService.find(condition, pageNum, pageSize);
        List<Provider> providerList = this.providerService.findByProName();
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("providerName", condition.getProductName());
        model.addAttribute("providerId", condition.getProviderId());
        model.addAttribute("isPayment", condition.getIsPayment());
        model.addAttribute("providerList", providerList);
        return "bill/billlist";
    }

    /**
     * 查看
     */
    @RequestMapping("/toView")
    public String toView(Bill bill, Model model) {
        model.addAttribute("bill", bill);
        return "bill/billview";
    }

    public void techProvider(Model model) {
        model.addAttribute("providerList", this.providerService.findByProName());
    }

    /**
     * 添加
     */
    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        techProvider(model);
        return "bill/billadd";
    }

    @RequestMapping("/doAdd")
    public String doAdd(@Valid Bill bill, BindingResult result, Model model) {
        if (result.hasErrors()) {
            techProvider(model);
            return "bill/billadd";
        }
        int r = this.billService.add(bill);
        if (r < 0) return "redirect:toAdd";
        return "redirect:show";
    }

    /**
     * 修改
     */
    @RequestMapping("/toModif")
    public String toModif(@Valid Bill bill, Model model) {
        List<Provider> providerList = this.providerService.findByProName();
        model.addAttribute("providerList", providerList);
        model.addAttribute("bill", bill);
        return "bill/billmodify";
    }

    @RequestMapping("/doModif")
    public String doModif(@Valid Bill bill, BindingResult result, Model model) {
        if (result.hasErrors()) {
            techProvider(model);
            return "bill/billmodify";
        }
        int r = this.billService.modif(bill);
        if (r < 0) return "redirect:toModif?flag=-1";
        return "redirect:show";
    }

    /**
     * 删除
     */
    @RequestMapping("/doRemove")
    public void doRemove(Long id, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        int r = this.billService.remove(id);
        out.print(r);
    }

    /**
     * 查询订单编码是否存在
     */
    @RequestMapping("/doBillCode")
    @ResponseBody
    public String doBillCode(String billCode) {
        Integer count = this.billService.findByBillCode(billCode);
        return count.toString();
    }
}
